package com.qf.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class RabbitConfig {

    @Bean
    public Queue queue(){
        return new Queue("email-queue");
    }

    //定义延迟队列用于订单的取消操作
    @Bean(name = "pay-queue")
    public Queue payQueue(){
        return new Queue("pay-queue");
    }

    @Bean(name = "dead-pay-queue")
    public Queue deadPayQueue(){

        Map map = new HashMap<>();
        map.put("x-dead-letter-exchange","pay-exchange");
        map.put("x-dead-letter-routing-key","pay");
        return new Queue("dead-pay-queue",true,true,false,map);
    }

    @Bean(name = "deadPayExchange")
    public DirectExchange directExchange(){
        return new DirectExchange("dead-pay-exchange");
    }
    @Bean(name = "PayExchange")
    public DirectExchange normaldirectExchange(){
        return new DirectExchange("pay-exchange");
    }

    @Bean
    Binding bindDeadQueueToDeadExchange(@Qualifier("dead-pay-queue")Queue queue,@Qualifier("deadPayExchange")DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("dead-pay");
    }

    @Bean
    Binding bindQueueuToExchange(@Qualifier("pay-queue")Queue queue,@Qualifier("PayExchange")DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("pay");
    }

}
